package com.ruitu.mall_98_seller.support.util;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;

import java.io.File;

/**
 * @author newtrekWang
 * @fileName FileProviderUtils
 * @createDate 2019/2/14 11:47
 * @email 408030208@qq.com
 * @desc FileProvider工具类, 用于适配Android7.0的文件访问变更
 */
public final class FileProviderUtils {
    /**
     * 获取合适的File Uri
     *
     * @param context
     * @param file
     * @return
     */
    public static Uri getUriForFile(Context context, File file, String authority) {
        Uri fileUri = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            fileUri = getUriForFile24(context, file, authority);
        } else {
            fileUri = Uri.fromFile(file);
        }
        return fileUri;
    }

    /**
     * 获取File Uri from 安卓7及以上版本
     */
    public static Uri getUriForFile24(Context context, File file, String authority) {
        Uri fileUri = FileProvider.getUriForFile(context, authority, file);
        return fileUri;
    }

    /**
     * 用户安装apk场景
     */
    public static void setIntentDataAndType(Context context, Intent intent, String type, File file, String authority) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            intent.setDataAndType(getUriForFile(context, file, authority), type);
        } else {
            intent.setDataAndType(Uri.fromFile(file), type);
        }
    }
}
